#!/usr/bin/php
<?php
if ($argc < 6) {
    exit(1);
}

$host=$argv[1];
$port=$argv[2];
$user=$argv[3];
$password=$argv[4];
$charset=$argv[5];
$db=$argv[6];
$sql=trim($argv[7], '"');

$connect = mysql_connect("$host:$port", $user, $password) or die("数据库连接错误");
mysql_query("SET NAMES '$charset'");
mysql_select_db($db, $connect);
$result = mysql_query($sql);
if ('resource' ==  gettype($result)) {
    while ($row = mysql_fetch_row($result)) {
        for ($i=0; $i < count($row); $i++) {
            if ($i != 0) {
                echo "\t";
            }
            echo $row[$i];
        }
        echo "\n";    
    }
} else {
    if ($result) {
        echo 1;
    }
}

mysql_close($connect);
?>
